package SIM_rrd;

#Copyright 2006 - 2010 Bernard van de Koppel
#This file is part of Sipman
#Sipman is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#Sipman is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with Sipman.  If not, see <http://www.gnu.org/licenses/>.
#
require Exporter;
use SIM_settings;
use SIM_base;
use RRDs;
use strict;

our @ISA 	= ("Exporter");
our @EXPORT	= qw( 
                     displayRrdGraph createRrdTable
                     );
our @VERSION	= 0.01;			# version number

# include variabeles to be used over the various modules here.
#
#
# ------------------- exportable subs -------------------------

sub  displayRrdGraph {
  my $graphDir = "$_[0]";
  my $graphFile = "$_[1]";
  my $period = "$_[2]";
  my $title = "$_[3]";
  my $vertLabel = "$_[4]";
  my $comment = "$_[5]";
  my $varInfo = "$_[6]";
  my $width = "$_[7]";
  my $peak = "$_[8]";
  my $factor = "$_[9]";
  my @colorTable = qw( 000000 00FF00 0000FF FF0000 FFFF00 FF1493 2F4F4F 8B0000 9400D3 00CED1 8FBC8F D2691E D2B48C );
  my $start;
  if ( $width eq "" ) {
    $width = 640;
  }
  if ( $peak eq "" ) {
    $peak = "AVERAGE";
  }
  if ( $factor eq "" ) {
    $factor = 1;
  }
  my $outFile = "$graphFile-$period-$width-$peak.png";
  my $lastupdate = RRDs::last "$graphDir/$graphFile.rrd";
  # set the time to the current time if we get nothing returned.
# if ( $lastupdate eq "" ) {
#   $lastupdate = 900;
# }

  if ( $period eq "week" ) {
    $start = $lastupdate - 604800;
  } elsif ( $period > 2000000 ) {
    $lastupdate = $period;
    $start = $lastupdate - 86400;
  } else {
    # if undefined use day.
    if ( $lastupdate eq "" ) {
      $lastupdate = time;
    }
    $start = $lastupdate - 86400;
  }
  my $statusDate = printDate( $lastupdate, "logdate" );
  $statusDate =~ s/:/./g;
  printDebug(1, "displayRrdGraph: lastupdate = $lastupdate, start = $start factor = $factor.");
  # create the graph
  my @graphInfo = ();
  push(@graphInfo, "$SIMparam{'SIMgraphDir'}/$outFile");
  push(@graphInfo, "--title=$title");
  push(@graphInfo, "--vertical-label=$vertLabel");
  push(@graphInfo, "--start=$start");
  push(@graphInfo, "--end=$lastupdate");
  push(@graphInfo, "--width=$width");
  my @varNames = split(';', $varInfo);
  my $numLine = 1;
  my $lineType;
  foreach my $entry (@varNames) {
    if ( $numLine eq "1" ) {
      $lineType = "AREA";
    } else {
      $lineType = "LINE1";
    }
    push(@graphInfo, "DEF:line$numLine=$graphDir/$graphFile.rrd:var$numLine:$peak");
    push(@graphInfo, "CDEF:nLine$numLine=line$numLine,$factor,*");
    push(@graphInfo, "$lineType:nLine$numLine#$colorTable[$numLine]:$entry");
    $numLine++;
  }
  push(@graphInfo, "COMMENT:$peak graph. Generated by Sipman, last update $statusDate.");
  RRDs::graph(@graphInfo);
  my $msg = "<IMG SRC=/graphs/$outFile>";
  my $err=RRDs::error;
  if ($err) {
    $msg = "$err, --start=\"$start\" --end=\"$lastupdate\" period=$period";
  }
  return("$msg");
}

sub createRrdTable {
  my $dataDir = "$_[0]";
  my $dataFile = "$_[1]";
  my $dataType = "$_[2]";
  my $count = "$_[3]";;
  my $rrdFile = "$dataDir/$dataFile.rrd";
  if ( ! -f $rrdFile ) {
    printDebug( 0, "createRrdTable: dataDir=$dataDir, dataFile=$dataFile, dataType=$dataType, count=$count. ");
    testMkDir $dataDir;
    my $lineInfo = "";
    my $num = 1;
    until ( $num > $count ) {
      $lineInfo = "$lineInfo DS:var$num:$dataType:1800:0:U";
      $num++;
    }
    system(" rrdtool create $rrdFile $lineInfo RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797 RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 RRA:MAX:0.5:24:775 RRA:MAX:0.5:288:797 ");
    print(" done. ");
  }
}

1;
